package com.shankungfu.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.nostra13.universalimageloader.utils.L;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * 各种phone信息接口 例如 imei 手机型号，系统 ，分辨率，应用包及包名
 * 
 * @author xugang
 */
public class PhoneUtils {
	private static final String TAG = "PhoneUtils";

	/** 读取Asset 里面的Html文件内容 */
	public static String getFromAssets(Context context, String fileName) {
		try {
			InputStreamReader inStreamReader = new InputStreamReader(context
					.getResources().getAssets().open(fileName));
			BufferedReader bufReader = new BufferedReader(inStreamReader);
			String line = "";
			StringBuffer sb = new StringBuffer();
			while ((line = bufReader.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";

	}

	/**
	 * 获取屏幕的宽和高的工具类 WangQing 2013-8-12 DisplayMetrics
	 * 
	 * @param context
	 * @return
	 */
	public static DisplayMetrics getAppWidthAndHeight(Context context) {
		// 这个可以用于1.5
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		return dm;
	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 将普通日期“2011/07/29 14:50:11” 转换成 unix时间戳
	 * 
	 * @param data
	 * @return
	 */
	public static long dataToUnixTimestamp(String data) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("", Locale.CHINA);
			Date date = df.parse(data);
			long date_long = date.getTime();
			return date_long;
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;
		}

	}

	/**
	 * 将时间戳转换成固定的日期
	 * 
	 * 填充格式：yyyy/MM/dd hh:mm:ss
	 * 
	 * @param data
	 * @return
	 */
	public static String TimeStamp2Date(String timestamp, String formats) {
		if (TextUtils.isEmpty(timestamp)) {
			return "";
		}
		Long time = Long.parseLong(timestamp) * 1000;
		SimpleDateFormat format = new SimpleDateFormat(formats, Locale.CHINA);
		String date = format.format(new Date(time));
		return date;
	}
	
	/**
	 * 将时间戳转换成固定的日期
	 * 
	 * 填充格式：yyyy/MM/dd hh:mm:ss
	 * 
	 * @param data
	 * @return
	 */
	public static String TimeStamp2DateForSKF(String timestamp) {
		return TimeStamp2Date(timestamp, "yyyy/MM/dd");
	}
	

}
